<?php

namespace App\Admin\Controllers;

use App\Models\FourAdminUser;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class FourAdminPswdController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new FourAdminUser(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('username');
            $grid->column('password');
            $grid->column('account');
            $grid->column('phone');
            $grid->column('role_id');
            $grid->column('status');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new FourAdminUser(), function (Show $show) {
            $show->field('id');
            $show->field('username');
            $show->field('password');
            $show->field('account');
            $show->field('phone');
            $show->field('role_id');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new FourAdminUser(), function (Form $form) {
            $form->password('password','新密码');
            $form->password('repassword','密码确认')->same('password', '两次密码输入不一致');
            $form->ignore('repassword');
            $form->saved(function (Form $form) {
                $password=$form->password('password','新密码');
                // 修改
                $form->updates(['password'=>$password]);
                return $form->response()->success('保存成功')->redirect('user');
            });
        });
    }
}
